import 'package:flame/components.dart';
import 'package:flutter_flappy_bird/bird.dart';

class PipeUpComponent extends SpriteComponent {
  PipeUpComponent(this.pipeHeight, this.addNewCallback, this.birdComponent,
      [this.onHalfPassedCallback])
      : super(size: Vector2(52, pipeHeight));

  final double pipeHeight;
  final BirdComponent birdComponent;
  late Function addNewCallback;
  late Vector2 screenSize;
  Function? onHalfPassedCallback;

  @override
  Future<void>? onLoad() async {
    sprite = await Sprite.load('pipe_down.png',
        srcSize: Vector2(52, pipeHeight),
        srcPosition: Vector2(0, 1000 - pipeHeight));
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    screenSize = size;
    position = Vector2(size.x, 0);
  }

  // 判断鸟是否已经撞击到柱子
  bool _containBird() {
    Vector2 birdPosition = birdComponent.getCurrentPosition();
    if (containsPoint(Vector2(birdPosition.x + 12, birdPosition.y)) ||
        containsPoint(Vector2(birdPosition.x, birdPosition.y - 12))) {
      return true;
    } else if (birdComponent.getCurrentStatus() ==
        BirdComponent.statusGameOver) {
      return true;
    } else {
      return false;
    }
  }

  @override
  void update(double dt) {
    super.update(dt);
    if (position.x <= -50) {
      removeFromParent();
    } else {
      if (_containBird()) {
        birdComponent.changeStatus(BirdComponent.statusGameOver);
      } else {
        position.sub(Vector2(1, 0));
        // 判断是否已经过半（得分）
        if (position.x <= screenSize.x / 2) {
          if (onHalfPassedCallback != null) {
            onHalfPassedCallback!();
            onHalfPassedCallback = () {};
          }
        }
        // 添加新的管道组
        if (screenSize.x - position.x >= 150) {
          addNewCallback();
          addNewCallback = () {};
        }
      }
    }
  }
}

class PipeDownComponent extends SpriteComponent {
  PipeDownComponent(this.pipeHeight, this.birdComponent)
      : super(size: Vector2(52, pipeHeight));

  final double pipeHeight;
  final BirdComponent birdComponent;

  late Vector2 screenSize;

  @override
  Future<void>? onLoad() async {
    sprite = await Sprite.load('pipe_up.png',
        srcSize: Vector2(52, pipeHeight), srcPosition: Vector2(0, 0));
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    screenSize = size;
    position = Vector2(size.x, size.y / 2 + screenSize.y / 2 - 96 - pipeHeight);
  }

  // 判断鸟是否已经撞击到柱子
  bool _containBird() {
    Vector2 birdPosition = birdComponent.getCurrentPosition();
    if (containsPoint(Vector2(birdPosition.x + 12, birdPosition.y)) ||
        containsPoint(Vector2(birdPosition.x, birdPosition.y + 12))) {
      return true;
    } else if (birdComponent.getCurrentStatus() ==
        BirdComponent.statusGameOver) {
      return true;
    } else {
      return false;
    }
  }

  @override
  void update(double dt) {
    super.update(dt);
    if (position.x <= -50) {
      removeFromParent();
    } else {
      if (_containBird()) {
        birdComponent.changeStatus(BirdComponent.statusGameOver);
      } else {
        position.sub(Vector2(1, 0));
      }
    }
  }
}
